package com.baseframe.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import com.baseframe.mybatis.handler.BaseMetaObjectHandler;
import com.baseframe.mybatis.plugins.SqlLogInterceptor;
import com.baseframe.mybatis.propes.BaseMybatisPlusProperties;
import com.baseframe.mybatis.utils.BulkUtil;
import lombok.AllArgsConstructor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@AllArgsConstructor
@MapperScan("com.baseframe.**.mapper")
@EnableConfigurationProperties({BaseMybatisPlusProperties.class})
public class MybatisPlusConfiguration {


    /**
     * 分页插件
     * @return mybatisPlusInterceptor 拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * sql 日志
     */
    @Bean
    public SqlLogInterceptor sqlLogInterceptor(BaseMybatisPlusProperties mybatisPlusProperties) {
        return new SqlLogInterceptor(mybatisPlusProperties);
    }

    @Bean
    public MetaObjectHandler metaObjectHandler(){

        return new BaseMetaObjectHandler();
    }

    @Bean
    public BulkUtil bulkUtil(SqlSessionFactory sqlSessionFactory){

        return new BulkUtil(sqlSessionFactory);
    }


}
